package cn.edu.cxcyzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.cxcyzx.algorithm.CalProgress_project;
import cn.edu.cxcyzx.entity.ApplyProject;
import cn.edu.cxcyzx.entity.Group;
import cn.edu.cxcyzx.entity.Progress_project;
import cn.edu.cxcyzx.entity.Project;
import cn.edu.cxcyzx.service.ApplyProjectService;
import cn.edu.cxcyzx.supervisor.DaoFit;
import cn.edu.cxcyzx.supervisor.ResultDo;

@Service("ApplyProjectService")
public class ApplyProjectServiceImpl implements ApplyProjectService {
	
	@Resource
	public DaoFit daoFit;
	
	@Override
	public void insertApply(ApplyProject applyProject) {
		// TODO Auto-generated method stub
		if(applyProject!=null){
			this.daoFit.getApplyProjectDao().insertApply(applyProject);
			System.out.println("iinsert_appPro————");
		}
			
	}

	@Override
	public int getApplyNum() {
		// TODO Auto-generated method stub
		return this.daoFit.getApplyProjectDao().getApplyNum();
	}

	@Override
	public int getApplyNumBy_liederId(String leaderId) {
		// TODO Auto-generated method stub
		int num=this.daoFit.getApplyProjectDao().getApplyNumBy_liederId(leaderId);
		System.out.println("num:"+num);
		return num;
	}

	@Override
	public ApplyProject getApplyBy_leaderId(String leaderId) {
		// TODO Auto-generated method stub
		if(leaderId!=null&&!"".equals(leaderId.trim())){
			return daoFit.getApplyProjectDao().getApplyBy_leaderId(leaderId);
		}
		return null;
	}

	@Override
	public ResultDo<List<ApplyProject>> getAllApply() {
		// TODO Auto-generated method stub
		ResultDo<List<ApplyProject>> resultDo=new ResultDo<List<ApplyProject>>();
		List<ApplyProject> list=daoFit.getApplyProjectDao().getAllApply();
		if(list==null){
			resultDo.setSuccess(false);
		}else{
			resultDo.setSuccess(true);
			resultDo.setResult(list);
		}
		return resultDo;
	}
	
	/**
	 * 处理项目申报，申报通过后：
	 * 						1.修改apply_project表中项目状态
	 * 						2.将项目信息插入project表
	 * 						3.项目申报人默认为项目组长，项目小组信息插入到projectleader表
	 * 						4.project_progress表中插入数据
	 */
	@Transactional
	@Override
	public void updateStatus(ApplyProject applyProject,String roomId) {
		// TODO Auto-generated method stub
		if(applyProject!=null){
			//1.修改apply_project表中项目状态
			if(applyProject.getProjectStatus()==1){
				daoFit.getApplyProjectDao().updateStatus(applyProject);
			}else if(applyProject.getProjectStatus()==2){
				daoFit.getApplyProjectDao().failProject(applyProject);
				return;
			}
			applyProject =this.daoFit.getApplyProjectDao().getProject_byProjectId(applyProject.getProjectId());
			if(applyProject.getProjectStatus()==1){
				//如果申报通过，将项目信息插入到project表,projectleader表
				Integer id=this.daoFit.getGroupDao().getId();
				String groupId="group"+(201800+(id==null?0:id)+1);
				this.daoFit.getProjectDao().insertApplyPro(new Project(applyProject,groupId,roomId));
				//更新房间剩余空位
				this.daoFit.getRoomDao().downRest(roomId, applyProject.getMemberNum());
				System.out.println("insert_pro————");
				this.daoFit.getGroupDao().insertGroup(new Group(applyProject, groupId));
				//4.project_progress表中插入数据
				Progress_project progress_project=new Progress_project(applyProject.getProjectId(),CalProgress_project.CREATE_PREJECT, "立项成功");
				this.daoFit.getProgress_projectDao().insertRecord(progress_project);
			}
		}
	}

	@Override
	public int getId() {
		// TODO Auto-generated method stub
		Integer id=this.daoFit.getApplyProjectDao().getId();
		return id==null?0:id;
	}
	
	/**
	 * 待处理申报项目数量
	 */
	@Override
	public int getApplyNum_0() {
		// TODO Auto-generated method stub
		return daoFit.getApplyProjectDao().getApplyNum_0();
	}

}
